package net.huashitong.synthetical.newSupervision.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.synthetical.newSupervision.entity.TaskNew;
import net.huashitong.synthetical.newSupervision.filter.TaskNewFilter;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/2
 * Time: 20:59
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Repository
public class TaskNewDao extends StringDao<TaskNew> {

    /**
     * 主查询
     *
     * @param filter
     * @return
     */
    public List<TaskNew> getProjectList(TaskNewFilter filter) {
        GenericQuery query = createQuery("obj");
        if (filter.getSysId() != null && !"".equals(filter.getSysId()))
            query.append(" and sysId=:sysId").setParam("sysId", filter.getSysId());
        if (filter.getTaskName() != null && !"".equals(filter.getTaskName()))
            query.append(" and taskName like:taskName").setParam("taskName", StringUtils.getLikewords(filter.getTaskName()));
        if (filter.getCategoryId() != null && !"".equals(filter.getCategoryId()))
            query.append(" and categoryId=:categoryId").setParam("categoryId", filter.getCategoryId());
        if (filter.getReferenceNumber() != null && !"".equals(filter.getReferenceNumber()))
            query.append(" and referenceNumber=:referenceNumber").setParam("referenceNumber", filter.getReferenceNumber());
        if (filter.getLeader() != null && !"".equals(filter.getLeader()))
            query.append(" and leader like:leader").setParam("leader", StringUtils.getLikewords(filter.getLeader()));
        if (filter.getInspectionId() != null && !"".equals(filter.getInspectionId()))
            query.append(" and inspectionId =:inspectionId").setParam("inspectionId", filter.getInspectionId());
        if (filter.getOperator() != null && !"".equals(filter.getOperator()))
            query.append(" and operator like:operator").setParam("operator", StringUtils.getLikewords(filter.getOperator()));
        if (filter.getType() != null && !"".equals(filter.getType()))
            query.append(" and type=:type").setParam("type", filter.getType());
        if (filter.getStatus() != null && !"".equals(filter.getStatus()))
            query.append(" and status =:status").setParam("status", filter.getStatus());
        if (StringUtils.isNotBlank(filter.getDateTimeStart()))
            query.append(" and dateTime >= :dateTimeStart").setParam("dateTimeStart", filter.getDateTimeStart());
        if (StringUtils.isNotBlank(filter.getDateTimeEnd()))
            query.append(" and dateTime <= :dateTimeEnd").setParam("dateTimeEnd", filter.getDateTimeEnd());

        //设置排序
        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                filter.getPaging().setSortCol("sysId");
                filter.getPaging().setSortVal("desc");
            }
        }

        return query.listResult(filter);
    }

}
